/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netease.xone.activity;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import com.netease.log.NTLog;
import com.netease.xone.R;


public class MovieViewControl implements MediaPlayer.OnErrorListener,
        MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener{

    @SuppressWarnings("unused")
    private static final String TAG = "MovieViewControl";

    private static final int ONE_MINUTE = 60 * 1000;
    private static final int TWO_MINUTES = 2 * ONE_MINUTE;
    private static final int FIVE_MINUTES = 5 * ONE_MINUTE;

    // Copied from MediaPlaybackService in the Music Player app. Should be
    // public, but isn't.
    private static final String SERVICECMD =
            "com.android.music.musicservicecommand";
    private static final String CMDNAME = "command";
    private static final String CMDPAUSE = "pause";

    private final VideoView mVideoView;
    private final View mProgressView;
    private final Uri mUri;
//    private final ContentResolver mContentResolver;

    // State maintained for proper onPause/OnResume behaviour.
    private int mPositionWhenPaused = -1;
    private boolean mWasPlayingWhenPaused = false;
    private MediaController mMediaController;

//    Handler mHandler = new Handler();


    public MovieViewControl(View rootView, Context context, Uri videoUri) {
        mVideoView = (VideoView) rootView.findViewById(R.id.surface_view);
        mProgressView = rootView.findViewById(R.id.progress_indicator);

        mUri = videoUri;

        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this);

        mMediaController = new MediaController(context);
        mVideoView.setMediaController(mMediaController);

        // make the video view handle keys for seeking and pausing
        mVideoView.requestFocus();
        mVideoView.setVideoURI(mUri);
        mVideoView.start();
    }

    

    public void onPause() {
//        mHandler.removeCallbacksAndMessages(null);
        mPositionWhenPaused = mVideoView.getCurrentPosition();
        mWasPlayingWhenPaused = mVideoView.isPlaying();
//        mVideoView.stopPlayback();
        mVideoView.pause();
    }

    public void onResume() {
        if (mPositionWhenPaused >= 0) {
//            mVideoView.setVideoURI(mUri);
//            mVideoView.seekTo(mPositionWhenPaused);
            if (mWasPlayingWhenPaused) {
                mVideoView.start();
                mVideoView.seekTo(mPositionWhenPaused);
                mMediaController.show(0);
            }
            mPositionWhenPaused = -1;
        }
    }

    @Override
    public boolean onError(MediaPlayer player, int arg1, int arg2) {
//        mHandler.removeCallbacksAndMessages(null);
        mProgressView.setVisibility(View.GONE);
        NTLog.e(TAG, "onError arg1 "+arg1 + " arg2" + arg2);
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        onCompletion();
    }

    public void onCompletion() {
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        NTLog.i(TAG, "onPrepared arg1");
        mProgressView.setVisibility(View.GONE);
    }
}
